require 'open-uri'

class MailController < ApplicationController  
  
  def send_question        
   if is_captcha_pass(params[:session], params[:capcha_text])
     FaqMailer.deliver_question(params[:question])
     flash[:notice] = 'Ваше сообщение отправлено.'
     session[:question] = ''
     redirect_to(service_06_static_pages_path)     
   else  
#     FaqMailer.deliver_question(params[:question])
     session[:question] = params[:question]
     flash[:warning] = 'Неверно введен код.'
     redirect_to(service_06_static_pages_path)
   end  
  end
  
  private
  
  def is_captcha_pass(session, answer)
    session = session.to_i
    answer  = answer.gsub(/\W/, '')
    open("http://captchator.com/captcha/check_answer/#{session}/#{answer}").read.to_i.nonzero? rescue false
  end

end
